#ifndef _UTILS_H_
#define _UTILS_H_

#include <fstream>
#include <string>
#include <ctime>
#include <algorithm>
#include <functional> 
#include <cctype>
#include <locale>

using namespace std;

namespace util
{
	class Utils
	{
	public:

		struct delete_object
		{
			template<typename T> void operator()(T *p){ if (p){ delete p; p = NULL;}}
		};

		static inline bool fileExists(string filepath)
		{
			ifstream aux(filepath);
			return aux.is_open();
		}
		
		static inline string timeStamp()
		{
			# define TIME_SIZE 40
			std::time_t now = std::time(NULL);
			const struct std::tm * tm_ptr = std::localtime(&now);
			char *s = new char[TIME_SIZE];

			size_t len = std::strftime(s, TIME_SIZE, "%Y/%m/%d %X ", tm_ptr);
			return string(s);
			# undef TIME_SIZE
		}

		// trim from begin
		static inline string &ltrim(string &s) 
		{
			s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
			return s;
		}

		// trim from end
		static inline std::string &rtrim(string &s) 
		{
			s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
			return s;
		}

		// trim from both ends
		static inline string &trim(string &s) 
		{
			return ltrim(rtrim(s));
		}
		
	};

};

#endif